﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using HostTool.Protocol;
using HostTool.Util;
using System.IO;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace HostTool.Core
{
    /// <summary>
    /// 机车信号主机类
    /// </summary>
  public  class HostMachine
    {
        List<IBoard> listBoard = new List<IBoard>();

        FrameManager frameManager;

       // SerialPort serial = new SerialPort();

        public HostMachine()
        {
            //串口设置
            //serial.BaudRate = 115200;
            frameManager = new FrameManager();
            //板卡设置


            string filePath = Path.Combine(Application.StartupPath, "Config.ini");
            if (File.Exists(filePath))
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    IniDocument doc = new IniDocument(fs);

                    this.ConfigDoc = doc;
                }
            }
            

            //listBoard.Add(new BoardHost(this, 0));
            //listBoard.Add(new BoardHost(this, 1));
            //listBoard.Add(new BoardRecord(this));
            //string verPath = Path.Combine(Application.StartupPath, "Version.txt");
            //if (File.Exists(verPath))
            {
                listBoard.Add(new BoardLink(this));
            }
            
      
            //listBoard.Add(new BoardTestBox(this));

        }


        public IniDocument ConfigDoc
        {
            get;
           private set;
        }

        public bool IsRunning
        {
            get;
            private set;
        }


        public string PortName
        {
            get {return "COM1" ; }
            set
            {
                
            }
        }

        public void Start()
        {
            try
            {
                IsRunning = true;

                foreach (IBoard board in listBoard)
                {
                    board.Start();
                }

            }
            catch (Exception) { }
            
        }

        public void Stop()
        {
            try
            {
                //if (serial.IsOpen)
                //{
                //    serial.Close();
                //}
                IsRunning = false;

            }
            catch (Exception) { }
        }



        public IList<IBoard> Boards
        {
            get { return listBoard.AsReadOnly(); }
        }


        public FrameManager FrameManager
        {
            get { return frameManager; }
        }


        private UdpClient udpcSend;

        public void SendData(byte[] data,int length)
        {
            if (IsRunning)//true)//serial.IsOpen)
            {
                try
                {
                    // 实名发送
                    IPEndPoint localIpep = new IPEndPoint(
                        IPAddress.Parse("192.168.0.120"), 12345); // 本机IP，指定的端口号
                    udpcSend = new UdpClient(localIpep);
                    IPEndPoint remoteIpep = new IPEndPoint(
                    
                        IPAddress.Parse("192.168.0.110"), 5555); // 发送到的IP地址和端口号

                    udpcSend.Send(data,length, remoteIpep);
                    udpcSend.Close();

                    //serial.Write(data, 0, length);
                }
                catch (Exception)
                { 
                }
            }
        }

    }
}
